<?php
$objXajax->registerFunction("frmmenutab_initPage");
$objXajax->registerFunction("frmmenutab_loadGrid");
$objXajax->registerFunction("frmmenutab_save");
$objXajax->registerFunction("frmmenutab_edit");
  
function frmmenutab_initPage(){
  global $objDbUpdate,$strCheck,$lang,$path;
    $objResponse = new xajaxResponse();
    $strCheck = frmmenutab_createTblHTMLLevel();
    $objResponse->addAssign('frmmenutab_trLevel', 'innerHTML', $strCheck);
    $strMainMenu = frmmenutab_createOptionFather();
    $objResponse->addAssign('frmmenutab_tdMainmenu', 'innerHTML', $strMainMenu);  
    //$arr = getArrService();
/*    $oTbs = new clsTinyButStrong();
    $oTbs->LoadTemplate('templates/'.$lang.'/'.$path.'/main_menutab_service_grid.html');
    $oTbs->MergeBlock('blk_data', $arr);   */
/*    $oTbs->Show(TBS_NOTHING);
    $objResponse->addAssign('frmmenutab_serviceType', 'innerHTML', $oTbs->Source); */
  return    $objResponse->getXML();             
}
function frmmenutab_save($params){
 global $objDbUpdate;
 $objResponse = new xajaxResponse(); 
    
 $name_vi = addslashes($params['txtViMenu']);
 $name_en = addslashes($params['txtEnMenu']);
 $module = addslashes($params['txtModule']);
 $ordershow = $params['txtOrder'];
 $menu_type =  $params['menuType'];
 //$service_type =  $params['cbbService']==''?0:$params['cbbService'];
 $scope =  $params['scope'];
/* if($scope==0){
   $service_type=0;  
 }*/
 $package_type =  $params['chklevel']; 
 $mainmenu =  $params['mainmenu'];
 $edit = $params['hdEdit'];
 $id = $params['hdId'];
 if($edit!=1){
    $sqlInsert = "INSERT INTO tbl_menu_cus(name_vi,name_en,module,menu_type,main_menu_id,active,date_created,date_update,ordershow,package_type) 
    VALUE('$name_vi','$name_en','$module','$menu_type','$mainmenu','0',now(),now(),'$ordershow','$package_type')";  
    $result    =  $objDbUpdate->Execute($sqlInsert);     
 }else{
     $sql = "UPDATE tbl_menu_cus SET name_vi ='$name_vi',name_en ='$name_en',module ='$module',ordershow ='$ordershow',
                menu_type ='$menu_type',main_menu_id='$mainmenu',date_update = now()
                ,package_type='$package_type' WHERE id = '$id'";
    if($objDbUpdate->Execute($sql)){
    }     
 }
 $objResponse->addScript("xajax_frmmenutab_loadGrid('');"); 
 return   $objResponse->getXML($sqlInsert);
}
 
function frmmenutab_createOptionFather(){
global $objDbSelect,$lang;
  $sql = "SELECT id AS id, name_$lang as name from tbl_main_menu_cus  ";
  $arrfather = $objDbSelect->GetArray($sql);
  $temp ='';
  $count = count($arrfather);
  for($i=0;$i<$count;$i++){
    $id = $arrfather[$i]['id'];
    $name = $arrfather[$i]['name'];
    $temp.= "<option value='$id'> $name </option>"  ;
  }
  $temp =  '<select id="frmmenutab_mainmenu" class="cbbRequired"><option value="">--Chọn--</option>'.$temp.'</select>';
  return $temp;
}

function frmmenutab_createTblHTMLLevel(){
  global $objDbSelect;
  $lang = $_SESSION['lang'];  
  $sql =          " select id,name_$lang as name from tbl_service_level";
  $arrMenu = $objDbSelect->GetArray($sql);
  $count = count($arrMenu);
  $tbl ="<table id='tblLevel' style='padding-left: 20px' class='width100'>";
  $checked ="checked='checked'";
  for($i=0;$i<$count;$i++){
    $id = $arrMenu[$i]['id'];
    $name = $arrMenu[$i]['name'];
    $tbl .= "<tr><td  style='padding-left: 10px'><input type='radio'  class='chkbox' name='frmmenutab_chklevel' $checked  id='frmmenutab_chklevel$id' value='$id'  />$name</td></tr>";   
    $checked='';
  }
  return $tbl.'</table>';
}

function frmmenutab_loadGrid($main_menu_id=''){     
    global $lang,$objDbSelect,$path;
    $lang ='vi';
    $arrMenu = frmmenutab_GetMenu($main_menu_id);
    $objResponse = new xajaxResponse();
    $oTbs = new clsTinyButStrong();
    $oTbs->LoadTemplate('templates/'.$lang.'/'.$path.'/main_menutab_grid.html');
    $oTbs->MergeBlock('blk_data', $arrMenu);
    $oTbs->Show(TBS_NOTHING);
    $objResponse->addAssign('frmmenutab_divGrid', 'innerHTML', $oTbs->Source); 
    $objResponse->addScriptCall("dataTableObj.CreatDataTableGroup('frmmenutab_tbl');"); 
    $objResponse->addScript('changeWaiting(1);');                              
    return $objResponse->getXML();
}

function  frmmenutab_GetMenu($main_menu_id=''){
    global $objDbSelect,$lang;
    $addCond = '';
    if($main_menu_id!=''){
        $addCond = " AND main_menu_id = '$main_menu_id' ";
    }
        $sqlSelect = "  SELECT me.id,me.ordershow,me.name_vi,me.name_en,me.module,me.active,if(me.menu_type=0,'Cơ bản','Nâng cao')
    as menu_type,ma.name_$lang as main_menu,l.name_$lang as levelname FROM tbl_menu_cus me join tbl_main_menu_cus ma 
    on me.main_menu_id = ma.id left join tbl_service_level as l on me.package_type = l.id  WHERE 1 = 1 $addCond 
    ORDER BY me.main_menu_id,me.ordershow";
    return $objDbSelect->GetArray($sqlSelect);
}

function  frmmenutab_edit($id){
    global $objDbSelect;
    $objResponse = new xajaxResponse();
    $sql         = "select * from tbl_menu_cus where id = '$id'";
    $arrMenu         = $objDbSelect->GetArray($sql);         
    $name_vi = $arrMenu[0]['name_vi'];
    $name_en = $arrMenu[0]['name_en'];
    $main_menu_id = $arrMenu[0]['main_menu_id'];
    $module = $arrMenu[0]['module'];
    $menu_type = $arrMenu[0]['menu_type'];
    $ordershow = $arrMenu[0]['ordershow'];
    
    //$scope = $arrMenu[0]['scope'];
/*    $service_type = $arrMenu[0]['service_type'];  */
    $package_type = $arrMenu[0]['package_type'];
    /*$scope=  $service_type==0?0:1; */
  
    $objResponse->addScript("$('#frmmenutab_txtViMenu').val('$name_vi');");
    $objResponse->addScript("$('#frmmenutab_txtEnMenu').val('$name_en');");
    $objResponse->addScript("$('#frmmenutab_mainmenu').val('$main_menu_id');");
    $objResponse->addScript("$('#frmmenutab_txtModule').val('$module');");
    $objResponse->addScript("$('#frmmenutab_txtOrder').val('$ordershow');");
    //$objResponse->addScript("$('#frmmenutab_cbbService').val($service_type);");
    $objResponse->addScript("$('#frmmenutab_menuType$menu_type').attr('checked',true);");
    $objResponse->addScript("$('#frmmenutab_chklevel$package_type').attr('checked',true);");
    /*$objResponse->addScript("$('#frmmenutab_service$scope').attr('checked',true);");  */
    /*$objResponse->addScript("frmmenutab_showService($scope);");  */
/*    if($service_type!=0){
        $objResponse->addScript("$('#frmmenutab_trServiceType').show();");            
    }else{
        $objResponse->addScript("$('#frmmenutab_trServiceType').hide();");              
    } */
    $objResponse->addScript('changeWaiting(1);');
    
    return $objResponse->getXML();
}   # end region process data
?>
